﻿using System;
using System.Linq;
using GalaSoft.MvvmLight.Messaging;
using Holdem.Interfaces.Attributes.Config;
using Holdem.Interfaces.Configuration;
using Holdem.Interfaces.Messages;
using Holdem.Interfaces.Model;
using Holdem.Interfaces.StartingHands;


namespace Holdem.Model
{
	public class StartingHandsModel : IStartingHandsModel
	{
		private readonly string _defaultStartingHand;
        private readonly IStartingHandsManager _startingHandsManager;
        private readonly IHoldemPhoneConfiguration _persistentConfiguration;

		private string SelectedStartingHandConfig
		{
			get
			{
				var savedConfig =
                    _persistentConfiguration.Cast<string>(ConfigKey.HoldemHandsViewSelectedStartingHand) ??
					_defaultStartingHand;

				return savedConfig;
			}
		}

		public StartingHandsModel(
			IStartingHandsManager startingHandsManager,
            [PersistentConfiguration] IHoldemPhoneConfiguration persistentConfiguration)
		{
            if (startingHandsManager == null)
                throw new ArgumentNullException("startingHandsManager");
            if (persistentConfiguration == null)
                throw new ArgumentNullException("persistentConfiguration");

            _startingHandsManager = startingHandsManager;
            _persistentConfiguration = persistentConfiguration;

            _defaultStartingHand = _startingHandsManager.AvailableStartingHandsByTitle.FirstOrDefault();
        }
		
        public void GenerateStartingHands()
        {
            var hand = _startingHandsManager.GetHandFromTitle(SelectedStartingHandConfig);

            Messenger.Default.Send(new StartingHandGeneratedMessage(hand));
        }
	}
}